package me.sdevil507.biz.fruit.service.impl;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitWxAppletDTO;
import me.sdevil507.biz.fruit.po.FruitWxAppletExPO;
import me.sdevil507.biz.fruit.po.QFruitWxAppletExPO;
import me.sdevil507.biz.fruit.po.QFruitWxAppletPO;
import me.sdevil507.biz.fruit.repository.FruitWxAppletExRepository;
import me.sdevil507.biz.fruit.service.FruitWxAppletExService;
import me.sdevil507.supports.jpa.PageParamsDTO;
import me.sdevil507.supports.jpa.SortParamsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class FruitWxAppletExServiceImpl implements FruitWxAppletExService {

    @Autowired
    FruitWxAppletExRepository fruitWxAppletExRepository;

    @Autowired
    JPAQueryFactory jpaQueryFactory;

    @Override
    public void save(FruitWxAppletExPO fruitWxAppletExPO) {
        fruitWxAppletExRepository.save(fruitWxAppletExPO);
    }

    @Override
    public Page<FruitWxAppletDTO> getWxAppletFruitOfPage(FruitWxAppletDTO searchDto, PageParamsDTO pageDto, SortParamsDTO sortDto) {

        // 执行返回
        Pageable pageable = pageDto.getPageable();

        QFruitWxAppletPO qFruitWxAppletPO = QFruitWxAppletPO.fruitWxAppletPO;
        QFruitWxAppletExPO qFruitWxAppletExPO = QFruitWxAppletExPO.fruitWxAppletExPO;
        QueryResults<FruitWxAppletDTO> queryResults = jpaQueryFactory.select(
                Projections.constructor(FruitWxAppletDTO.class,
                        qFruitWxAppletPO.content,
                        qFruitWxAppletPO.picUrl,
                        qFruitWxAppletExPO.id,
                        qFruitWxAppletExPO.name,
                        qFruitWxAppletExPO.mobile,
                        qFruitWxAppletExPO.type,
                        qFruitWxAppletExPO.address,
                        qFruitWxAppletExPO.pubTime,
                        qFruitWxAppletExPO.theme,
                        qFruitWxAppletExPO.author,
                        qFruitWxAppletExPO.organization,
                        qFruitWxAppletExPO.summary,
                        qFruitWxAppletExPO.ocrText,
                        qFruitWxAppletExPO.keyword,
                        qFruitWxAppletExPO.wxAppletFruitId,
                        qFruitWxAppletPO.createdDate
                )
        ).from(qFruitWxAppletExPO).leftJoin(qFruitWxAppletPO).on(qFruitWxAppletExPO.wxAppletFruitId.eq(qFruitWxAppletPO.id))
                .orderBy(qFruitWxAppletExPO.createdDate.desc())
                .offset(pageable.getPageNumber() * pageable.getPageSize())
                .limit(pageable.getPageSize())
                .fetchResults();

        return  new PageImpl<>(queryResults.getResults(), pageable, queryResults.getTotal());
    }
}
